package com.rc.web.service.impl;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.web.utils.VivoAuth;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class QueryService {

    public String QueryRewrite(String first ,String second , String third) throws IOException {
        // 配置参数
        String appId = "2025985389"; // 替换为您的APP_ID
        String appKey = "eAkRKtYXOawsmtVv"; // 替换为您的APP_KEY
        String uri = "/query_rewrite_base";
        String method = "POST";
        String domain = "api-ai.vivo.com.cn";
        String url = "https://" + domain + uri;

        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        List<List<String>> prompts = new ArrayList<>();

        // 添加历史对话
        List<String> historyPrompt = new ArrayList<>();
        historyPrompt.add(""); // q3, 上三轮问题
        historyPrompt.add(""); // a3, 上三轮回答
        historyPrompt.add(""); // q2, 上两轮问题
        historyPrompt.add(""); // a2, 上两轮回答
        historyPrompt.add(first); // q1, 上一轮问题
        historyPrompt.add(second); // a1, 上一轮回答

        // 添加当前问题
        List<String> currentPrompt = new ArrayList<>();
        currentPrompt.add(third); // q, 当前轮问题

        prompts.add(historyPrompt);
        prompts.add(currentPrompt);
        requestBody.put("prompts", prompts);

        // 将请求体转换为JSON字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonBody = objectMapper.writeValueAsString(requestBody);
        System.out.println("请求体: " + jsonBody);

        // 生成认证头
        HttpHeaders headers = VivoAuth.generateAuthHeaders(appId, appKey, method, uri, "");
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 打印请求头
        VivoAuth.printHeaders(headers);

        // 发送请求
        RestTemplate restTemplate = new RestTemplate();
        HttpEntity<String> request = new HttpEntity<>(jsonBody, headers);

        try {
            String response = restTemplate.postForObject(url, request, String.class);
            System.out.println("响应结果: " + response);
            
            // 解析响应JSON，提取result数组中的内容
            Map<String, Object> responseMap = objectMapper.readValue(response, Map.class);
            if (responseMap.containsKey("result") && responseMap.get("result") instanceof List) {
                List<String> resultList = (List<String>) responseMap.get("result");
                if (!resultList.isEmpty()) {
                    return resultList.get(0); // 返回result数组中的第一个元素
                }
            }
            
            return "无法解析响应结果";
        } catch (Exception e) {
            System.err.println("请求失败: " + e.getMessage());
            e.printStackTrace();
            return "请求失败: " + e.getMessage();
        }
    }


}
